import Dialog from '@vant/weapp/dialog/dialog';
import api from "../../api/index";
import { formatDate } from "../../utils/util";

const getDataFuncMap: { [key: string]: (data: { date: string[] }) => Promise<any> } = {
  feedLoss: api.getFeedLoss,
  divide: api.getDivide,
  sale: api.getSale,
  'sale-a': api.getSaleA,
  clearLoss: api.getClearLoss,
  put: api.getPut
}
const typeTitleMap: { [key: string]: string } = {
  feedLoss: '食耗记录',
  divide: '分塘记录',
  sale: '销售记录',
  'sale-a': 'A鳗销售记录',
  clearLoss: '清塘损耗记录',
  put: '新苗投塘记录',
}

// pages/history/history.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    typeOptions: [
      { text: '食耗记录', value: 'feedLoss' },
      { text: '分塘记录', value: 'divide' },
      { text: '销售记录', value: 'sale' },
      { text: 'A鳗销售记录', value: 'sale-a' },
      { text: '清塘损耗记录', value: 'clearLoss' },
      { text: '新苗投塘记录', value: 'put' },
    ],
    type: 'feedLoss',
    date: formatDate(new Date()),
    recordList: [],
    loading: true,
    maxDate: new Date().getTime(),
    minDate: new Date('2024-01-01').getTime()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.onGetNewRecord(this.data.type, this.data.date);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   *  日期选择
   */
  onDateConfirm(event: any) {
    this.selectComponent('#date-drop-down').toggle();
    const date = formatDate(event.detail);
    this.onGetNewRecord(this.data.type, date);
  },
  /**
   * 类型切换
   */
  onTypeChange(event: any) {
    const newType = event.detail;
    this.onGetNewRecord(newType, this.data.date);
  },
  onCellClick(event: any) {
    const record = event.currentTarget.dataset.record;
    let message = `塘号：${record.reportBasic.poolNo}\n上报人：${record.reportBasic.reporter}\n发生日期：${formatDate(record.reportBasic.date)}\n上报日期：${formatDate(record.reportBasic.reportDate)}`;
    switch (this.data.type) {
      case 'feedLoss':
        message += `\n投料量：${record.feed} kg\n损耗量：${record.loss} 尾\n损耗重量：${record.lossWeight} kg`
        break;
      case 'divide':
        message = `分出塘号：${record.poolNoOut}\n分入塘号：${record.poolNoIn}\n上报人：${record.reportBasic.reporter}\n发生日期：${formatDate(record.reportBasic.date)}\n上报日期：${formatDate(record.reportBasic.reportDate)}\n分出数量：${record.quantity} 尾`
        break;
      case 'sale':
        message += `\n销售量：${record.quantity} 尾\n销售重量：${record.weight} kg`
        break;
      case 'sale-a':
        message += `\n销售量：${record.quantity} 尾\n销售重量：${record.weight} kg`
        break;
      case 'clearLoss':
        message += `\n清塘损耗数量：${record.quantity} 尾\n清塘损耗重量：${record.weight} kg`
        break;
      case 'put':
        message += `\n投苗数量：${record.quantity} 尾\n投苗重量：${record.weight} kg`
        break;
    }
    return Dialog.alert({
      title: '详细上报数据',
      message
    }).then(() => {

    })
  },
  onGetNewRecord(type: string, date: string) {
    this.setData({
      loading: true
    });
    const managePoolNos = getApp().globalData.userInfo.managePoolNos;
    getDataFuncMap[type]({ date: [date, date] }).then(res => {
      const data = res.data;
      const managedData = data.filter((record: any) => {
        return managePoolNos.includes(record.reportBasic.poolNo);
      });
      const recordList = managedData.map((item: any) => {
        return { ...item, title: `${item.reportBasic.poolNo}塘 ${typeTitleMap[type]} ${date}` }
      });
      this.setData({
        recordList,
        loading: false,
        date,
        type
      })
    })
  }
})